package com.ibm.risk.irmp.common.exception;

import jakarta.annotation.Resource;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.WebRequest;

import java.util.Map;

@RestController
public class GlobalErrorController implements ErrorController {
    // 错误请求路径
    private final String ERROR_PATH = "/error";

    @Resource
    private ErrorAttributes errorAttributes;


    /**
     * JSON格式错误信息
     */
    @RequestMapping(value = ERROR_PATH)
    public Map<String, Object> error(WebRequest webRequest) {
        ErrorAttributeOptions options = ErrorAttributeOptions.of(ErrorAttributeOptions.Include.MESSAGE);
        Map<String, Object> body = this.errorAttributes.getErrorAttributes(webRequest, options);
        return body;
    }
}
